package com.yanqu.road.server.cmd.activity.gourdchild;


import com.yanqu.road.entity.activity.gourdchild.GourdChildConfig;
import com.yanqu.road.entity.activity.gourdchild.GourdChildUserData;
import com.yanqu.road.pb.activity.GourdChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.gourdchild.GourdChildModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.gourdchild.GourdChildMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_GOURD_CHILD_MOVE_GRID_TRAIN, desc = "移动（练习模式）")
public class GourdChildTrainMoveGridCmd extends AbstractCommand {

    int direction;

    private GourdChildUserData userData = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GourdChildProto.GourdChildMoveGridReqMsg reqMsg = GourdChildProto.GourdChildMoveGridReqMsg.parseFrom(packet.getBytes());
        direction = reqMsg.getDirection();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        GourdChildConfig config = GourdChildMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_OPEN;
            return;
        }

        GourdChildModule module = player.getModule(GourdChildModule.class);
        userData = module.getUserData2();
        if (userData == null) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_UNLOCK;
            return;
        }

        ret = module.moveGridTrain(direction);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            GourdChildProto.GourdChildMoveGridTrainRespMsg.Builder respMsg = GourdChildProto.GourdChildMoveGridTrainRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_GOURD_CHILD_MOVE_GRID_TRAIN, respMsg);
        }
    }


}
